package gps.monitor.common.tools.network;

import gps.monitor.common.core.network.AbstractConnectionConfig;
import gps.monitor.common.core.network.ISocketClient;
import gps.monitor.common.core.network.ISocketServer;
import gps.monitor.common.core.network.NetworkException;
import io.netty.channel.ChannelHandler;

/**
 * @author cyq
 * @date 2021/11/4
 * @desc talk is cheep,show me the code
 */
public final class SocketConnectorFactory {

    public static ISocketClient createOneClient(AbstractConnectionConfig config, ChannelHandler encoder) throws NetworkException {

        if (config == null) throw new NetworkException("请指定配置信息：AbstractConnectionConfig");
        if (encoder == null) throw new NetworkException("请指定编码器：encoder");
        if (config instanceof SimpleNettyConnectConfig) {
            return new SimpleNettyClient((SimpleNettyConnectConfig) config, encoder);
        }
        throw new NetworkException("指定的服务器类型暂无支持：" + config.getClass().getName());
    }

    public static ISocketServer createOneServer(AbstractConnectionConfig config,
                                                ChannelHandler decoder,
                                                ChannelHandler handler) throws NetworkException {

        if (config == null) throw new NetworkException("请指定配置信息：AbstractConnectionConfig");
        if (decoder == null) throw new NetworkException("请指定解码器：codec");
        if (handler == null) throw new NetworkException("请指定处理器：handler");
        if (config instanceof SimpleNettyConnectConfig) {
            return new SimpleNettyServer((SimpleNettyConnectConfig) config, decoder, handler);
        }
        throw new NetworkException("指定的服务器类型暂无支持：" + config.getClass().getName());
    }
}
